#!/bin/bash
# This script is meant to be called as part of a UDEV rule.  Its purpose is simply
# to execute usbremap with a given Ruby script.  Essentially, to autostart usbremap
# whenever the device is plugged in.  The script must not wait for usbremap to finish,
# but instead must simply start it and then exit (we don't want to hold up UDEV).
# Also, we only start usbremap if it is not already running (in my experience, UDEV
# tends to execute its RUN directives multiple times when devices are plugged in).

PROC=$( pstree -p | grep usbremap )
DIR=$( echo $0 | sed "s/\(.*\)\/[^\/]*/\1/" )
if [ -z "$PROC" ]; then
  nohup $DIR/usbremap $DIR/scripts/ag5-gaming.rb </dev/null &>/dev/null 2>&1 &
fi
